﻿<div id="mainTasks">
Task Lists
</div>
<script type="text/javascript">

    $(document).ready(function () {

        $("#mainTasks").text("");
        $("#mainTasks").append("<div class='loading'><img src='../../Content/Loading/loading2.gif' /></div>");

        var url = '@Url.Action("LoadTasks", "Task")';
        $.ajax({
            url: url,
            type: 'GET',
            cache: true,
            success: function (result) {

                $("#mainCategory").hide();
                $("#mainTasks").text("");

                var dateTimeLabel, dateTimeNoLabel;

                for (var i in result) {
                    var taskTitle = result[i].TaskTitle;
                    var taskId = result[i].TaskId;
                    var dateScheduledJson = result[i].TaskScheduled;

                    var dateScheduled, d;
                    if (dateScheduledJson == null) {
                        dateTimeNoLabel = dateScheduled;
                        if (dateTimeNoLabel != "No Date") {
                            dateScheduled = "No Date";
                            $("#mainTasks").append("<div class='dateLabel' >" + dateScheduled + "</div>");
                        }
                    }
                    else {
                        var milli = dateScheduledJson.replace(/\/Date\((-?\d+)\)\//, '$1');
                        d = new Date(parseInt(milli));
                        dateScheduled = "Scheduled for : " + d.toDateString();
                    }

                    if (dateTimeLabel != d.toDateString()) {
                        dateTimeLabel = d.toDateString();
                        $("#mainTasks").append("<div class='dateLabel' >" + dateScheduled + "</div>");
                    }

                    $("#mainTasks").append("<div class='tasks' >" + taskTitle + "<br/> <div style='float:right'> <div class='doneBtn' data-taskid='" + taskId + "'>done</div> <div class='editBtn' data-taskid='" + taskId + "'>edit</div> <div class='deleteBtn' data-taskid='" + taskId + "'>delete </div> <div class='dateBtn'>" + dateScheduled + "</div>  </div> </div>");
                }

                if (result.length == 0) {
                    $("#mainTasks").text("Empty !");
                }

                $("#mainCategory").fadeIn(400);
            },
            error: function () {
                $("#mainTasks").text("");
                $("#mainTasks").append("Error occured !");
            }
        });
    });

</script>

